﻿Ext
		.onReady(function() {
			Ext.QuickTips.init();
			Ext.form.Field.prototype.msgTarget = 'under';
			var system = new Ext.data.Record.create( [ {
				name : 'systems_id',
				type : 'int'
			}, {
				name : 'systems_name',
				type : 'string'
			}, {
				name : 'systems_code',
				type : 'string'
			}, {
				name : 'systems_sysFlag',
				type : 'int'
			}, ])
			var systemsStore = new Ext.data.Store( {
				proxy : new Ext.data.HttpProxy( {
					url : '/schedule/querySystems'
				}),
				reader : new Ext.data.JsonReader( {
					totalProperty : 'totalProperty',
					root : 'root'
				}, system),
				remoteSort : true
			});
			systemsStore.load( {
				params : {
					start : 0,
					limit : 30
				}
			});

			var sm = new Ext.grid.CheckboxSelectionModel();

			var cols = new Ext.grid.ColumnModel( [ new Ext.grid.RowNumberer(),
					sm, {
						header : '专业名称',
						dataIndex : 'professional_name'
					}, {
						header : '所属学院',
						dataIndex : 'professional_systems.systems_name'
					} ]);
			cols.defaultSortable = true;

			var professional = new Ext.data.Record.create( [ {
				name : 'professional_id',
				type : 'int'
			}, {
				name : 'professional_systems.systems_name',
				type : 'string'
			}, {
				name : 'professional_systems.systems_id',
				type : 'int'
			}, {
				name : 'professional_code',
				type : 'string'
			}, {
				name : 'professional_name',
				type : 'string'
			}, {
				name : 'professional_sysFlag',
				type : 'int'
			}, ]);

			var professionalstore;
			function getData(val) {
				var url = "/schedule/queryProfessional?flag="
						+ encodeURI(encodeURI(val));
				professionalstore = new Ext.data.Store( {
					id : "professional_id",
					proxy : new Ext.data.HttpProxy( {
						url : url
					}),
					reader : new Ext.data.JsonReader( {
						totalProperty : 'totalProperty',
						root : 'root'
					}, professional),
					remoteSort : true
				});
				if (val != undefined) {
					grid.reconfigure(professionalstore, cols);
				}
				professionalstore.load( {
					params : {
						start : 0,
						limit : 15
					}
				});
			}
			;
			getData();

			var openwin = function() {
				var professionalform = new Ext.form.FormPanel(
						{
							region : 'center',
							frame : true,
							layout : "form", // 整个大的表单是form布局
							labelWidth : 65,
							labelAlign : "right",
							maximizable : true,
							items : [ { // 行1
										layout : "column", // 从左往右的布局
										items : [
												{
													columnWidth : .4,
													layout : "form",
													items : [ {
														xtype : "textfield",
														allowBlank : false,
														validateOnBlur : true,
														name : "professional.professional_name",
														fieldLabel : "专业名称",
														blankText : '必须填写',
														width : 120
													} ]
												},
												{
													columnWidth : .4,
													layout : "form",
													items : [ {
														xtype : 'combo',
														store : systemsStore,
														mode : 'local',
														valueField : "systems_id",
														forceSelection : true,
														hiddenName : 'professional.professional_systems.systems_id',
														displayField : "systems_name",
														editable : false,
														triggerAction : 'all',
														allowBlank : false,
														validateOnBlur : true,
														fieldLabel : '所属院系',
														width : 162
													} ]
												},
												{
													columnWidth : .4,
													layout : "form",
													items : [ {
														xtype : "hidden",
														blankText : '必须填写',
														name : "professional.professional_code",
														fieldLabel : "专业编号",
														width : 120
													} ]
												} ]
									}, {// 行4
										buttonAlign : "right",
										buttons : [
												{
													text : "提交",
													handler : function() {
														if (professionalform.form
																.isValid()) {
															professionalform.form
																	.submit( {
																		waitTitle : '请稍候',
																		waitMsg : '正在保存......',
																		url : '/schedule/addProfessional',
																		success : function(
																				form,
																				action) {
																			Ext.Msg
																					.alert(
																							"系统消息",
																							action.result.msg,
																							function() {
																								form
																										.reset();
																								win
																										.close();
																								professionalstore
																										.reload();
																								grid
																										.resumeEvents();
																							},
																							this);
																		},
																		failure : function(
																				form,
																				action) {
																			Ext.Msg
																					.alert(
																							'系统消息',
																							action.result.msg);
																			form
																					.reset();
																			win
																					.close();
																		}
																	})
														}

													}
												},
												{
													text : "重置",
													listeners : {
														click : function() {
															professionalform.form
																	.reset()
														}
													}
												} ]
									} ]

						});

				var win = new Ext.Window( {
					layout : 'fit',
					title : '专业信息',
					width : 650,
					height : 120,
					resizable : true,
					closeAction : 'close',
					plain : true,
					items : professionalform,
					listeners : {
						close : function(w) {
							w.restore();
						},
						maximize : function(w) {
						}
					}
				}).show();
			};
			var grid = new Ext.grid.GridPanel(
					{
						title : '专业信息列表',
						region : 'center',
						buttonAlign : "left",
						listeners : {
							"rowdblclick" : function(grid, rowIndex, e) {
								var records = grid.getSelectionModel()
										.getSelections();
								var systemsform = new Ext.form.FormPanel(
										{
											region : 'center',
											frame : true,
											layout : "form", // 整个大的表单是form布局
											labelWidth : 65,
											labelAlign : "right",
											maximizable : true,
											items : [ { // 行1
														layout : "column", // 从左往右的布局
														items : [
																{
																	columnWidth : .4,
																	layout : "form",
																	items : [ {
																		xtype : "textfield",
																		value : records[0]
																				.get("professional_name"),
																		allowBlank : false,
																		validateOnBlur : true,
																		name : "professional.professional_name",
																		fieldLabel : "专业名称",
																		blankText : '必须填写',
																		width : 120
																	} ]
																},
																{
																	columnWidth : .4,
																	layout : "form",
																	items : [ {
																		xtype : 'combo',
																		store : systemsStore,
																		mode : 'local',
																		valueField : "systems_id",
																		value : records[0]
																				.get("professional_systems.systems_id"),
																		forceSelection : true,
																		hiddenName : 'professional.professional_systems.systems_id',
																		displayField : "systems_name",
																		editable : false,
																		triggerAction : 'all',
																		allowBlank : false,
																		validateOnBlur : true,
																		fieldLabel : '所属院系',
																		width : 162
																	} ]
																},
																{
																	xtype : "hidden",
																	name : "professional.professional_id",
																	value : records[0]
																			.get("professional_id"),
																	allowBlank : false
																},
																{
																	xtype : "hidden",
																	name : "professional.professional_sysFlag",
																	value : records[0]
																			.get("professional_sysFlag"),
																	allowBlank : false
																},
																{
																	columnWidth : .4,
																	layout : "form",
																	items : [ {
																		xtype : "hidden",
																		allowBlank : false,
																		blankText : '必须填写',
																		value : records[0]
																				.get("professional_code"),
																		name : "professional.professional_code",
																		fieldLabel : "专业编号",
																		width : 120
																	} ]
																} ]
													}, {//行2
														buttonAlign : "right",
														buttons : [
																{
																	text : "修改",
																	handler : function() {
																		systemsform.form
																				.submit( {
																					waitTitle : '请稍候',
																					waitMsg : '正在保存......',
																					url : '/schedule/addProfessional',
																					method : "POST",
																					success : function(
																							form,
																							action) {
																						Ext.Msg
																								.alert(
																										"系统消息",
																										action.result.msg,
																										function() {
																											win
																													.close();
																											professionalstore
																													.reload();
																										},
																										this);
																					},
																					failure : function(
																							form,
																							action) {
																						Ext.Msg
																								.alert(
																										'系统消息',
																										action.result.msg);
																						win
																								.close();
																						professionalstore
																								.reload();
																					}
																				})

																	}
																},
																{
																	text : "重置",
																	listeners : {
																		click : function() {
																			systemsform.form
																					.reset()
																		}
																	}
																} ]
													} ]
										});
								var win = new Ext.Window( {
									layout : 'fit',
									title : '院系信息',
									width : 650,
									height : 120,
									resizable : true,
									closeAction : 'close',
									plain : true,
									items : systemsform,
									listeners : {
										close : function(win) {
											//关键部分：关闭窗口前先还原,滚动条才不会消失
										win.restore();
									},
									maximize : function(win) {
										//关键部分：最大化后需要将窗口重新定位，否则窗口会从最顶端开始最大化
										//w.setPosition(document.body.scrollLeft,document.body.scrollTop);
									}
									}
								});
								win.show();
							}
						},
						buttons : [
								{
									text : "新增",
									listeners : {
										click : function() {
											openwin();
										}
									}
								},
								{
									text : "删除",
									listeners : {
										click : function() {
											var ids = "";
											var records = grid
													.getSelectionModel()
													.getSelections();
											if ("" == records) {
												Ext.MessageBox.alert("提示",
														"请选择要删除的记录");
											} else {
												for ( var i = 0; i < records.length; i++) {

													ids += records[i]
															.get("professional_id")
															+ ",";

												}
												;
												Ext.MessageBox
														.confirm(
																"确认删除",
																"确认删除所选数据？",
																function(button) {
																	if (button == "yes") {
																		Ext.Ajax
																				.request( {
																					url : '/schedule/deleteProfessional',
																					params : {
																						ids : ids
																					},
																					method : 'POST',
																					success : function(
																							form,
																							action) {
																						Ext.Msg
																								.alert(
																										"系统消息",
																										"删除成功",
																										function() {
																											grid
																													.resumeEvents();
																											professionalstore
																													.reload();
																										},
																										this);
																					},
																					failure : function(
																							form,
																							action) {
																						Ext.Msg
																								.alert(
																										'系统消息',
																										"删除失败");
																						grid
																								.resumeEvents();
																						professionalstore
																								.reload();
																					},

																					scope : this
																				});
																	}
																}, this);
											}
										}
									}
								},
								{
									text : "修改",
									listeners : {
										click : function() {
											var record = grid
													.getSelectionModel()
													.getSelected();
											if (!record) {
												Ext.Msg.alert("提示",
														"请先选择要修改的行!");
												return;
											}
											var changeform = new Ext.form.FormPanel(
													{
														region : 'center',
														frame : true,
														layout : "form", // 整个大的表单是form布局
														labelWidth : 65,
														labelAlign : "right",
														maximizable : true,

														items : [ { // 行1
																	layout : "column", // 从左往右的布局
																	items : [
																			{
																				columnWidth : .5,
																				layout : "form",
																				items : [ {
																					xtype : "textfield",
																					name : "professional.professional_name",
																					value : record
																							.get("professional_name"),
																					allowBlank : false,
																					validateOnBlur : true,
																					fieldLabel : "专业名称",
																					width : 120
																				} ]
																			},
																			{
																				columnWidth : .4,
																				layout : "form",
																				items : [ {
																					xtype : 'combo',
																					store : systemsStore,
																					mode : 'local',
																					valueField : "systems_id",
																					value : record
																							.get("professional_systems.systems_id"),
																					forceSelection : true,
																					hiddenName : 'professional.professional_systems.systems_id',
																					displayField : "systems_name",
																					editable : false,
																					triggerAction : 'all',
																					allowBlank : false,
																					validateOnBlur : true,
																					fieldLabel : '所属院系',
																					width : 162
																				} ]
																			},
																			{
																				columnWidth : .5,
																				layout : "form",
																				items : [
																						{
																							xtype : "hidden",
																							name : "professional.professional_code",
																							value : record
																									.get("professional_code"),
																							allowBlank : false,
																							fieldLabel : "专业编号",
																							width : 120
																						},
																						{
																							xtype : "hidden",
																							name : "professional.professional_id",
																							value : record
																									.get("professional_id"),
																							fieldLabel : "专业ID",
																							width : 120
																						},
																						{
																							xtype : "hidden",
																							name : "professional.professional_sysFlag",
																							value : record
																									.get("sysFlag"),
																							fieldLabel : "数据标志",
																							width : 120
																						} ]
																			} ]
																}, {// 行3
																	buttonAlign : "right",
																	buttons : [
																			{
																				text : "保存修改",
																				handler : function() {
																					if (changeform.form
																							.isValid()) {
																						changeform.form
																								.submit( {
																									waitTitle : '请稍候',
																									waitMsg : '正在保存......',
																									url : '/schedule/addProfessional',
																									success : function(
																											form,
																											action) {
																										Ext.Msg
																												.alert(
																														"系统消息",
																														action.result.msg,
																														function() {
																															changeform.form
																																	.reset();
																															changewin
																																	.close();
																															grid
																																	.resumeEvents();
																															professionalstore
																																	.reload();
																														},
																														this);
																									},
																									failure : function(
																											form,
																											action) {
																										Ext.Msg
																												.alert(
																														'系统消息',
																														action.result.msg);
																										grid
																												.resumeEvents();
																										changeform.form
																												.reset();
																										changewin
																												.close();
																									}
																								})
																					}

																				}
																			},
																			{
																				text : "重置",
																				listeners : {
																					click : function() {
																						changeform.form
																								.reset()
																					}
																				}
																			} ]
																} ]

													});

											var changewin = new Ext.Window( {
												layout : 'fit',
												title : '专业信息',
												width : 650,
												height : 120,
												resizable : true,
												closeAction : 'close',
												plain : true,
												items : changeform,
												listeners : {
													close : function(w) {
														w.restore();
													},
													maximize : function(w) {

													}
												}
											});

											changewin.show();
										}

									}
								} ],
						loadMask : true,
						store : professionalstore,
						cm : cols,
						sm : sm,
						viewConfig : {
							forceFit : true
						},
						bbar : new Ext.PagingToolbar( {
							store : professionalstore,
							pageSize : 15,//页大小 
							displayInfo : true,//是否显示displayMsg 
							displayMsg : "本页显示第{0}条到第{1}条的记录,一共{2}条",
							emptyMsg : "没有记录"//如果没有记录，则显示该文本 
						})

					});

			var form = new Ext.form.FormPanel( {
				region : "north",
				id : "form",
				width : 650,
				height : 60,
				hideBorders : true,
				layout : "form", // 整个大的表单是form布局
				labelWidth : 80,
				labelAlign : "right",
				items : [ { // 行1
					layout : "column", // 从左往右的布局
					hideBorders : true,
					items : [ {
						columnWidth : .3,
						layout : "form",
						items : [ {
							xtype : 'combo',
							store : systemsStore,
							mode : 'local',
							valueField : "systems_name",
							forceSelection : true,
							hiddenName : 'flag0',
							displayField : "systems_name",
							editable : false,
							triggerAction : 'all',
							allowBlank : false,
							fieldLabel : '所属院系',
							width : 162
						} ]
					}, {
						columnWidth : .3,
						layout : "form",
						items : [ {
							xtype : "textfield",
							name : "flag1",
							fieldLabel : "专业名称",
							width : 80
						} ]
					} ]
				} ],
				tbar : [
						new Ext.Toolbar.TextItem('查询'),
						{
							xtype : "tbfill"
						},
						{
							pressed : true,
							text : '查询',
							listeners : {
								click : function() {
									Ext.StoreMgr.remove('professional_id');
									var flag0 = Ext.getCmp("form").getForm()
											.findField('flag0').getValue();
									var flag1 = Ext.getCmp("form").getForm()
											.findField('flag1').getValue();
									if (flag0 == "") {
										flag0 = "-";
									}
									;

									if (flag1 == "") {
										flag1 = "-";
									}
									;

									var flag = "" + flag0 + "," + flag1;
									getData(flag);//提交方法
								}
							}
						}, {
							xtype : "tbseparator"
						}, {
							pressed : true,
							text : '置空',
							listeners : {
								click : function() {
									form.form.reset();
								}
							}
						} ]
			});

			var viewport = new Ext.Viewport( {
				layout : 'border',
				title : '列表',
				items : [ form, grid, {
					region : 'south',
					contentEl : 'foot'
				} ]
			});
		});